function start(){
  
  var $ = go.GraphObject.make;
  var allnumber=0;
  var myDiagram =
    $(go.Diagram, "div1",
      {
        "undoManager.isEnabled": false,
        layout: $(go.TreeLayout,
                  { angle: 90, layerSpacing: 22 })
      });
  
  // the template we defined earlier
  myDiagram.nodeTemplate =
    $(go.Node, "Horizontal",
      { background: "#44CCFF" },
         $(go.TextBlock, "Default Text",
        { margin: 10, stroke: "white", font: "bold 10px consolas" },
        new go.Binding("text", "lan"))
    );
  
  // define a Link template that routes orthogonally, with no arrowhead
  myDiagram.linkTemplate =
    $(go.Link,
      { routing: go.Link.Orthogonal, corner: 5 },
      $(go.Shape, // the link's path shape
        { strokeWidth: 1, stroke: "#555" }));
  
  var model = $(go.TreeModel);
  model.nodeDataArray =
  [
    
  ];

  myDiagram.model = model;
 
  var BinarySearchTree = function(){
    this.root = null;
    ///
}


BinarySearchTree.prototype = {
    insert: function(keyvalue){//插入
        var newNode = new this.Node(keyvalue);
        //插入节点，基本没有什么结构变化
        if(this.root === null){
            this.root = newNode;
        }
        else{
            this.insertNode(this.root, newNode)
        }
        console.log(this.root);
    },
    
    Node: function(key){
        this.key = key;
        this.left = null;
        this.right = null;
    },
    insertNode: function(node, newNode){
        if(newNode.key < node.key){
            if(node.left === null){
                node.left = newNode;
               
            }else{
                this.insertNode(node.left, newNode)
            }
        }else{
            if(node.right === null)
            {
                node.right = newNode;
               
            }
            else{
                this.insertNode(node.right, newNode)
            }
        }
    },
   
    minNode: function(node){
        if(node){
            while(node && node.left !== null){
                node = node.left;
            }
            return node.key;
        }
        return null;
    },
    maxNode: function(node){
        if(node){
            while(node && node.right !== null){
                node = node.right;
            }
            return node.key;
        }
        return null;
    },
    searchNode: function(node, key){//bool type and only return for it true and false 
        if(node === null)
        return false;
        if(key < node.key){
            return this.searchNode(node.left, key);
        }else if(key > node.key){
            return this.searchNode(node.right, key);
        }else{
            return true;
        }
    },
    
    findMinNode: function(node){
        if(node){
            while(node && node.left !== null){
                node = node.left;
            }
            return node.key;
        }
        return null;
    },
    deleteWithKey:function(key)
    {
        this.deleteNodeWithKey(this.root,null,false,key);
    },
    deleteNodeWithKey:function(node,parnode,whetherleftin,key)
    {
        if(node.key==key)
        {
            if(node.left==null&&node.right==null)//被删除节点正好为叶子结点
            {
                if(parnode!=null)
                {
                    if(whetherleftin)
                    parnode.left=null;
                    else
                    parnode.right=null;
                }
               
                node=null;
                
                return 1;
            }
            if(node.left==null)//被删除的节点只有右侧有子树
            {
                if(whetherleftin)
                {
                    parnode.left=node.right;
                }
                else
                {
                parnode.right=node.right;
                }   
                node=null;
                return 1;
            }
            if(node.right==null)//被删除的节点只有右子树
            {
                if(whetherleftin)
                parnode.left=node.left;
                else
                parnode.right=node.left;
                
               
                node=null;
                return 1;
            }
            var mxskey=this.minNode(node.right);
            this.deleteNodeWithKey(node.right,node,false,mxskey);
            node.key=mxskey;
        }
        else
        {
            if(node.key>key)
            {
                if(node.left!=null)
                 this.deleteNodeWithKey(node.left,node,true,key);
                else
                return 0; 
            }
            else
            if(node.right!=null)
                this.deleteNodeWithKey(node.right,node,false,key);
            else
            return 0;    
        }
    },
    drawinto_bst:function(){
        myDiagram.model.addNodeData({key:this.root.key.toString(),lan:this.root.key.toString()});
        this.drwa_base(this.root);
    },
    drwa_base:function(node)
    {
        if(node.left!=null)
        {
            myDiagram.startTransaction("make new node");
            myDiagram.model.addNodeData({key:node.left.key.toString(),parent:node.key.toString(),lan:node.left.key.toString()});
            myDiagram.commitTransaction("make new node");
            this.drwa_base(node.left);
        }
        else
        {
        myDiagram.startTransaction("make new node");
        myDiagram.model.addNodeData({key:allnumber.toString(),parent:node.key.toString(),lan:".left"});
        myDiagram.commitTransaction("make new node");
        allnumber=allnumber+1;
        }
        if(node.right!=null)
        {
            myDiagram.startTransaction("make new node");
            myDiagram.model.addNodeData({key:node.right.key.toString(),parent:node.key.toString(),lan:node.right.key.toString()});
            myDiagram.commitTransaction("make new node");
            this.drwa_base(node.right);
        }
        else
        {
            myDiagram.startTransaction("make new node");
        myDiagram.model.addNodeData({key:allnumber.toString(),parent:node.key.toString(),lan:".right"});
        myDiagram.commitTransaction("make new node");        
        allnumber=allnumber+1;
        }
}
}
  mybst=new BinarySearchTree();
  function cleardata()
  {
      allnumber=0;
      while(myDiagram.model.nodeDataArray.length!=0)
      {
          myDiagram.startTransaction("remove node");
          myDiagram.model.removeNodeData(myDiagram.model.nodeDataArray[0]);
          myDiagram.commitTransaction("remove node");
      }
  }
  function refreshbst(){
        cleardata();
    mybst.drawinto_bst();
  }
  var func1=document.getElementById("de");
  func1.onclick=function()
  {
      console.log("clcijcs");
    myDiagram.model.addNodeData({key:"1",lan:"fresfes"});
  }
  var inhelp1=document.getElementById("inh1");
  var inhelp2=document.getElementById("inh2");
  inhelp1.onclick=function()
  {
      var kuang1=document.getElementById("kuang1");
      var innum=parseInt(kuang1.value);
      mybst.insert(innum);
      refreshbst();
  }
  inhelp2.onclick=function()
  {
      var kuang2=document.getElementById("kuang2");
      var innu=parseInt(kuang2.value);
      mybst.deleteWithKey(innu);
      refreshbst();
  }
 /* var nmd=document.getElementById("xyy1");
  nmd.onclick=function()
  {
    mybst.insert(114151);
  }
 
  var inxtrigger=document.getElementById("queren");
  inxtrigger.onclick=function()
  {  
    var inx=document.getElementById("data");
    var num=parseInt(inx.value);
    mybst.insert(num);

  }
  var anti=document.getElementById("q2");
  anti.onclick=function()
  {
      var inclave=document.getElementById("dat2");
      var num=parseInt(inclave.value);
      mybst.deleteWithKey(num);
  }*/
}
